package com.huishu.tcm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.huishu.tcm.entity.Annex9;
import com.huishu.tcm.entity.User;
import com.huishu.tcm.service.Annex9Service;
import com.huishu.tcm.util.exception.TcmException;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huishu.tcm.entity.Comparative;
import com.huishu.tcm.service.ComparativeService;
import com.huishu.tcm.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

@Api(tags = "对照组用户表")
@RestController
@RequestMapping("/api/comparative")
public class ComparativeController extends BaseController<ComparativeService> {

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询一条数据", notes = "根据主键查询一条数据")
    @RequestMapping(method = RequestMethod.GET)
    public Comparative get(@RequestParam String id) throws TcmException {
        QueryWrapper<Comparative> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return service.getOne(wrapper);
    }

    /**
     * 查询数据列表
     * @return
     */
    @ApiOperation(value = "根据筛选条件查询数据列表", notes = "根据筛选条件查询数据列表")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public List<Comparative> list(@RequestBody Comparative v) throws TcmException {
        QueryWrapper<Comparative> queryWrapper = service.getQueryWrapper(v);
        return service.list(queryWrapper);
    }

    /**
     * 分页查询数据列表
     * @param pageVo
     * @return
     */
    @ApiOperation(value = "根据筛选条件分页查询数据列表", notes = "根据筛选条件分页查询数据列表")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Page<Comparative> page(@RequestBody PageVo<Comparative> pageVo) throws TcmException {
        Comparative v = pageVo.getEntity();
        QueryWrapper<Comparative> wrapper = service.getQueryWrapper(v);
        Page<Comparative> page = new Page<>(pageVo.getPage(),pageVo.getLimit());
        page = service.page(page,wrapper);
        return page;
    }

    /**
     * 添加
     * @param v
     * @return
     */
    @ApiOperation(value = "添加一条数据",notes = "添加一条数据")
    @RequestMapping(method = RequestMethod.POST)
    public boolean save(@RequestBody Comparative v) throws Exception {
        Date now = new Date();
        User loginUser = getLoginUser();
        v.setCreateTime(now);
        v.setCreateUser(loginUser.getName());
        v.setUpdateTime(now);
        v.setUpdateUser(loginUser.getName());
        return service.save(v);
    }

    /**
     * 根据主键更新
     * @param v
     * @return
     */
    @ApiOperation(value = "根据主键更新数据",notes = "根据主键更新数据")
    @RequestMapping(method = RequestMethod.PUT)
    public boolean update(@RequestBody Comparative v) throws Exception {
        QueryWrapper<Comparative> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", v.getId());
        return service.update(v,queryWrapper);
    }


    /**
     * 根据主键删除
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键删除数据",notes = "根据主键删除数据")
    @RequestMapping(method = RequestMethod.DELETE)
    public boolean removeById(@RequestParam Integer id){
        QueryWrapper<Comparative> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return service.remove(wrapper);
    }

    @Resource
    Annex9Service annex9Service;

    @ApiOperation(value = "查询对照组糖尿病患者中医干预随访记录表末次填写是否完成", notes = "返回值：{annex9: true、false}")
    @GetMapping(value = "/queryAnnex7TheLastTimeIsOver", params = {"patientId"})
    public Map<String,Boolean> queryAnnex7TheLastTimeIsOver(int patientId){
        QueryWrapper<Annex9> annex9QueryWrapper = new QueryWrapper<>();
        annex9QueryWrapper.eq("patient_id", patientId);
        annex9QueryWrapper.eq("the_last_time", true);
        Annex9 annex9 = annex9Service.getOne(annex9QueryWrapper);
        Map<String, Boolean> map = new HashMap<>();
        map.put("annex9", false);
        if(ObjectUtil.isNotEmpty(annex9)){
            map.put("annex9",true);
        }
        return map;
    }

    /**
     * 对照组用户结束管理
     * @param patientId 对照组用户id
     */
    @ApiOperation(value = "对照组用户结束管理", notes= "对照组用户结束管理")
    @GetMapping(value = "/endManagementPatient", params = {"patientId"})
    public void endManagementPatient(int patientId){
        QueryWrapper<Comparative> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", patientId);
        Comparative comparative = service.getOne(queryWrapper);
        comparative.setEndManagement(true);
        service.update(comparative, queryWrapper);
    }
}